package com.hoshiicloud.as2c.minipro.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yangcheng
 * @version 1.0
 * @title: HtmlUtil
 * @description: 处理Html字符串工具类
 * @date 2019/6/5 19:44
 * @mail maiboyc@163.com
 */
public class HtmlUtil {

    public static String[] getImgs(String content) {
        Pattern p_image;
        Matcher m_image;
        String str = "";
        String[] images = null;
        String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(content);
        String img;
        while (m_image.find()) {
            img = m_image.group();
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String tempSelected = m.group(1);

                if ("".equals(str)) {
                    str = tempSelected;
                } else {
                    String temp = tempSelected;
                    str = str + "," + temp;
                }
            }
        }
        if (!"".equals(str)) {
            images = str.split(",");
        }
        return images;
    }

    public static void main(String[] args) {
        String desc = "<p style=\"text-align: center\">\n" +
                "<img src=\"http://imgyx.hoshiicloud.com/images/75296487-04d9-4791-9dd3-01a56de871e0.jpg\" style=\"\"/></p>\n" +
                "<p style=\"text-align: center\">\n" +
                "<img src=\"http://imgyx.hoshiicloud.com/images/3971f63e-18bb-46da-9b10-1f342a1b7386.jpg\" style=\"\"/></p>\n" +
                "<p style=\"text-align: center\">\n" +
                "<img src=\"http://imgyx.hoshiicloud.com/images/a3908238-e062-48bc-bae7-0b1f831ded24.jpg\" style=\"\"/></p>\n" +
                "<p style=\"text-align: center\">\n" +
                "<img src=\"http://imgyx.hoshiicloud.com/images/7de94a70-979d-473e-b3bb-0bbd7940092f.jpg\" style=\"\"/></p>\n" +
                "<p style=\"text-align: center\">\n" +
                "<img src=\"http://imgyx.hoshiicloud.com/images/449fe177-dbfd-4b3a-b0e9-62e90e5a8c81.jpg\" style=\"\"/></p>\n" +
                "<p><br/></p>";
        String[] img = getImgs(desc);
        for (String s : img) {
            System.out.println(s);
        }
    }

}
